//
//  Enum.swift
//  CCMoney_swift
//
//  Created by JKHuang on 2018/11/27.
//  Copyright © 2018 JKHuang. All rights reserved.
//

import Foundation
import UIKit

enum HHResult {
    case ok(message: String)
    case empty
    case failed(message: String)
}

extension HHResult {
    var isValid: Bool {
        switch self {
        case .ok:
            return true
        default:
            return false
        }
    }
}

// MARK: Dictionary to json
extension NSDictionary {
    func toJSON() -> String {
        
        var data: Data = Data()
        do {
            data = try JSONSerialization.data(withJSONObject: self, options: .init(rawValue: 0))
        }
        catch let error {
            print(error)
        }
        return String(data: data, encoding: .utf8) ?? ""
    }
}

// MARK: Dictionary to json
extension Dictionary {
    func toJSON() -> String {
        
        var data: Data = Data()
        do {
            data = try JSONSerialization.data(withJSONObject: self, options: .init(rawValue: 0))
        }
        catch let error {
            print(error)
        }
        return String(data: data, encoding: .utf8) ?? ""
    }
}

// MARK: Array to json
extension NSArray {
    func toJSON() -> String {
        var data: Data = Data()
        do {
            data = try JSONSerialization.data(withJSONObject: self, options: .init(rawValue: 0))
        }
        catch let error {
            print(error)
        }
        return String(data: data, encoding: .utf8) ?? ""
    }
}

// MARK: Timer
extension Timer {
    
    class func lj_scheduledTimer(timeInterval interval: TimeInterval, repeats: Bool, block: @escaping (Timer) -> Swift.Void) -> Timer {
        let timer = Timer.scheduledTimer(timeInterval: interval, target: self, selector: #selector(timeBlock(timer:)), userInfo: block, repeats: repeats)
        return timer
    }
    
    @objc class func timeBlock(timer: Timer) {
        let block = timer.userInfo as? (Timer) -> Swift.Void
        guard let action = block else {
            return
        }
        action(timer)
    }
}
// MARK: HHError
enum HHError: Error {
    case EmptyError
    case RequestError
}
// MARK: HHResponse
public enum HHResponse<T> {
    case Success(T)
    case Failed(Error)

}


extension HHResponse {
    
    var isSuccess: Bool {
        switch self{
        case .Success:
            return true
        case .Failed:
            return false
        }
    }
    
    var data: T? {
        switch self {
        case HHResponse.Success(let value): return value
        case HHResponse.Failed: return nil
        }
    }
    
    func map<U>(f: (T)->U) -> HHResponse<U> {
        switch self {
        case .Success(let t): return .Success(f(t))
        case .Failed(let err): return .Failed(err)
        }
    }
    func flatMap<U>(f: (T)->HHResponse<U>) -> HHResponse<U> {
        switch self {
        case .Success(let t): return f(t)
        case .Failed(let err): return .Failed(err)
        }
    }
}

